﻿@{
    ViewBag.Title = "UnitTest";
}

<h2>UnitTest</h2>

<div class="panel panel-default">
    <div class="panel-heading">
        <div class="panel-title">Need of Unit tests</div>
    </div>
    <div class="panel-body">
        <ul>
            <li>To validate that the developer's code works as expected</li>
        </ul>
    </div>
</div>

<div class="panel panel-default">
    <div class="panel-heading">
        <div class="panel-title">Visual Studio Unit Test Framework</div>
    </div>
    <div class="panel-body">
        <ul>
            <li>Microsoft.VisualStudio.TestTools.UnitTestFramework</li>
            <li>Not a part of the .Net framework, it is a part of Visual Studio</li>
            <li>
                Normal code, identified as unit tests by use of attributes
                <ul>
                    <li><code>[TestClass]</code></li>
                    <li><code>[TestMethod]</code></li>
                </ul>
            </li>
        </ul>
    </div>
</div>

<div class="panel panel-default">
    <div class="panel-heading">
        <div class="panel-title">Paradigms</div>
    </div>
    <div class="panel-body">
        <ul>
            Remember the "Three A's" pattern
            <li>Arrange : create your variables, fill your expected variables with expected values</li>
            <li>Act : Call "what you need to test" and get the actual values in your variables</li>
            <li>Assert : Compare the expected and actual values</li>
        </ul>
    </div>
</div>

<div class="panel panel-default">
    <div class="panel-heading">
        <div class="panel-title">Managing unit tests</div>
    </div>
    <div class="panel-body">
        Test->Windows->
        <ul>
            <li>
                Test View : All unit tests in flat list
                <img src="~/Images/TestView.png" /> Add more columns
            </li>
            <li>
                Test List Editor : Group and organize related tests
                <img src="~/Images/Test List Editor.png" /> Add more groups
            </li>
            <li>
                Test results : Displays the status of test execution
                <img src="~/Images/Test Results.png" /> Choose old test results to see old details.
            </li>
        </ul>
    </div>
</div>

<div class="panel panel-default">
    <div class="panel-heading">
        <div class="panel-title">Test Run Configuration</div>
    </div>
    <div class="panel-body">
        <ul>
            <li>
                Directly from code
                <ul>
                    <li>Right click on a test method and click "Run Tests"</li>
                    <li>Right click on a class and click "Run Test" to run all the test methods on that class</li>
                </ul>
            </li>
            <li>"Test tools" tool bar</li>
        </ul>
        Whenever we run these tests a group of settings are applied to them, these settings are called test configuration. These are .testsettings file. To choose from different .testsettings file goto Test->Select Active Test Settings->
    </div>
</div>

<div class="panel panel-default">
    <div class="panel-heading">
        <div class="panel-title">Asserts and Testing Lifecycle</div>
    </div>
    <div class="panel-body">
        <ul>
            <li>Initialization and Cleanup of Unit Tests</li>
            <li>Assertions</li>
            <li>Working with TestContext</li>
            <li>Data Driven Unit Tests</li>
        </ul>
    </div>
</div>

<div class="panel panel-default">
    <div class="panel-heading">
        <div class="panel-title">Initialization and Cleanup of Unit Tests</div>
    </div>
    <div class="panel-body">
        <ul>
            <li>
                Configure a resource shared among your tests
                <ul>
                    <li>Database connection</li>
                    <li>Shared Object</li>
                    <li>A Log file</li>
                </ul>
                Attributes available for shared resource at Test, Class &amp; Assembly level
            </li>
            <li>
                TestInitialize and TestCleanUp
                <ul>
                    <li><code>[TestInitialize]</code> &amp; <code>[TestCleanUp]</code> are attributes</li>
                    <li>Only one method in a class can be decorated with this attribute</li>
                    <li><code>[TestInitialize]</code> &amp; <code>[TestCleanUp]</code> are called before and after respectively of running <em>each</em>test method.</li>
                    <li>unit test methods do not run in a particular order</li>
                </ul>
            </li>
            <li>
                ClassInitialize and CleanUp
                <ul>
                    <li><code>[ClassInitialize]</code> &amp; <code>[ClassCleanUp]</code> are attributes</li>
                    <li>Only one method in a class can be decorated with this attribute</li>
                    <li>unit test methods do not run in a particular order</li>
                    <li><code>[TestInitialize]</code> &amp; <code>[TestCleanUp]</code> are called before and after respectively of running <em>all</em>test methods.</li>
                    <li>These have to be static methods</li>
                </ul>
            </li>
            <li>
                AssemblyInitialize and CleanUp
                <ul>
                    <li><code>[AssemblyInitialize]</code> &amp; <code>[AssemblyCleanUp]</code> are attributes</li>
                    <li>These are executed before any of the test methos in any test class in the assembly</li>
                    <li>Only one method in a class can be decorated with this attribute</li>
                    <li>Define this in any of the test classes</li>
                    <li>method should be static</li>
                </ul>
            </li>
        </ul>
    </div>
</div>

<div class="panel panel-default">
    <div class="panel-heading">
        <div class="panel-title">Assertion</div>
    </div>
    <div class="panel-body">
        <ul>
            To Compare expected and actual values. Three Assert classes provided by visual studio. These are all static types
            <li>
                Assert
                <ul>
                    <li>Compare two input values</li>
                    <li>Many methods with several overloads</li>
                    <li>AreEqual, AreSame, IsFalse, IsInstanceOf</li>
                </ul>
            </li>
            <li>
                CollectionAssert
                <ul>
                    <li>Compare two collections</li>
                    <li>Check items in collections</li>
                    <li>AllItemsAreNotNull, AllItemsAreUnique, AreEqual(order matters), AreEquivalent(order doesn't matter), IsSubsetOf</li>
                </ul>
            </li>
            <li>
                StringAssert
                <ul>
                    <li>Compare strings</li>
                    <li>Contains, Matches(second parameter is a regex), StartsWith, EndsWith</li>
                </ul>
            </li>
        </ul>
    </div>
</div>

<div class="panel panel-default">
    <div class="panel-heading">
        <div class="panel-title">TestContext</div>
    </div>
    <div class="panel-body">
        <ul>
           <li>Set by the runtime</li>
            <li>Provides information about the unit test run environment</li>
            <li>When testing web services stores the url of the web service</li>
            <li>When testing ASP.NET apps provides access to the <code>Page</code> object</li>
            <li>When using Data Driven tests provides access to the data source</li>
            <li>usage
            <ul>
                <li>just provide the property in the test class <code>public TestContext testContext {get; set;}</code> and the unit test runtime will automatically fill it up</li>
                <li><code> TestContext.TestRunDirectory</code></li>
                <li><code> TestContext.TestName</code></li>
            </ul>
            </li>
        </ul>
    </div>
</div>

<div class="panel panel-default">
    <div class="panel-heading">
        <div class="panel-title">Testing Web Applications</div>
    </div>
    <div class="panel-body">
        <ul>
            Types of tests
            <li>Functional tests</li>
            <li>Data-driven Tests</li>
            <li>Performance Tests</li>
        </ul>
    </div>
</div>
